package com.modouyu.novel.core.freemark;


import com.modouyu.novel.entity.SysDictEntity;
import com.modouyu.novel.service.SysDictService;
import freemarker.core.Environment;
import freemarker.template.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ${DESCRIPTION}
 *
 * @author yangcb
 * @email mudouyu@aliyun.com
 * @create 2017-11-24 13:33
 **/
@Component
public class ChannelList implements TemplateDirectiveModel {

    private Logger logger = LoggerFactory.getLogger(ChannelList.class);


    @Autowired
    private SysDictService sysDictService;


    @Override
    public void execute(Environment env, Map map, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
        logger.debug("模板map:{}", map);
        List<SysDictEntity> typeList = sysDictService.queryNovelType();
        DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);
        env.setVariable("channels", builder.build().wrap(typeList));
        body.render(env.getOut());
    }


}
